/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ufs.dcomp.mgemeos.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import ufs.dcomp.mgemeos.negocio.Local;

/**
 *
 * @author Luiz Marcus
 * @modificacao 19/02/11
 */
public class LocalDAO implements interface_dao.ILocal {

    private static String inserir = "insert into local (capacidade, endereco) values (?,?)";
    private static String atualizar = "update local set capacidade=?, endereco=? where idLocal=?";
    private static String obter = "select * from local where idLocal = ? ";
    private static String obter_todos = "select * from local";
    private static String delete = "delete from local where idLocal = ? ";

    @Override
    public boolean atualizarLocal(Local local) {
        Conexao conexao = new Conexao();
        Connection con = conexao.getConnection();
        PreparedStatement pst;
        try {
            pst = con.prepareStatement(atualizar);
            pst.setInt(1, local.getCapacidade());
            pst.setString(2, local.getEndereco());
            pst.setInt(3, local.getIdLocal());
            pst.executeUpdate();
            pst.close();
            con.close();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public Local obterLocal(int id) {
        Local local = null;
        Conexao conexao = new Conexao();
        Connection con = conexao.getConnection();
        PreparedStatement pst;
        try {
            pst = con.prepareStatement(obter);
            pst.setInt(1, id);
            ResultSet rs = pst.executeQuery();
            if (rs.next()) {

                local = new Local();
                local.setIdLocal(rs.getInt(1));
                local.setCapacidade(rs.getInt(2));
                local.setEndereco(rs.getString(3));

            }
            rs.close();
            pst.close();
            con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return local;
    }

    @Override
    public boolean inserirLocal(Local local) {
        Conexao conexao = new Conexao();
        Connection con = conexao.getConnection();
        PreparedStatement pst;  
            try {
                pst = con.prepareStatement(inserir);
                pst.setInt(1, local.getCapacidade());
                pst.setString(2, local.getEndereco());
                pst.execute();
                pst.close();
                con.close();
                return true;
            } catch (SQLException e) {
                e.printStackTrace();
                return false;
            }
        
    }

    @Override
    public boolean deletarLocal(int id) {
        Conexao conexao = new Conexao();
        Connection con = conexao.getConnection();
        PreparedStatement pst;

        Local local2 = obterLocal(id);

        if (local2 != null) {
            try {
                pst = con.prepareStatement(delete);
                pst.setInt(1, id);
                pst.execute();
                pst.close();
                con.close();
                return true;
            } catch (SQLException e) {
                e.printStackTrace();
                return false;
            }
        } else {
            return false;
        }
    }

    @Override
    public ArrayList<Local> obterLocal_todos() {
        Local local = null;
        ArrayList<Local> lista_organizadores = new ArrayList<Local>();
        Conexao conexao = new Conexao();
        Connection con = conexao.getConnection();
        PreparedStatement pst;
        try {
            pst = con.prepareStatement(obter_todos);
            ResultSet rs = pst.executeQuery();
            while (rs.next()) {

                local = new Local();
                local.setIdLocal(rs.getInt(1));
                local.setCapacidade(rs.getInt(2));
                local.setEndereco(rs.getString(3));
                
                lista_organizadores.add(local);
            }
            rs.close();
            pst.close();
            con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return lista_organizadores;
    }

    
}
